package com.hyxt.provider;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.hyxt.api.ChainLinkServer;
import com.hyxt.api.ILinkService;
import com.hyxt.service.IReceiveMsg;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @author songm
 * @version v1.0
 * @Description 链路管理接口现
 * @Date: Create in 18:18 2017/12/23
 * @Modifide By:
 **/

@Component
@Service(interfaceClass = ILinkService.class, version = "1.0.0")
public class LinkServiceImpl implements ILinkService {

    private static final Log log = LogFactory.getLog(LinkServiceImpl.class);
    //主链路名后缀
    private static final String mainLinkSuffix = "_ms";
    //转发链路名后缀
    private static final String transferLinkSuffix = "_tc";
    //转发链路名后缀
    private static final String slaveLinkSuffix = "_sc";

    @Autowired
    @Qualifier("receive809ClientMsg")
    private IReceiveMsg receiveMsgReceive809ClientMsg;

    @Autowired
    @Qualifier("receive809ServerMsg")
    private IReceiveMsg receiveMsgReceive809ServerMsg;

    @Autowired
    @Qualifier("receiveTransferClientMsg")
    private IReceiveMsg receiveMsgReceiveTransferClientMsg;


    @Reference(version = "1.0.0")
    private ChainLinkServer chainLinkConsumer;

    //交通部链路管理
    @Reference(version = "1.0.1", check = false)
    private ChainLinkServer chainLinkServer2;

    /**
     * 启动809服务端
     *
     * @param platId 平台id
     * @param port   端口号
     * @return 启动成功返回监听的通道名，否则返回null
     */
    @Override
    public String start(String platId, int port) {
        String channelName = platId + mainLinkSuffix;
        //todo 调用链路管理接口开启端口
        int integer = chainLinkConsumer.openPort(port, channelName);
        if (integer == 0) {
            //开启端口后开始监听
            if (receiveMsgReceive809ServerMsg.listen(channelName)) {
                return channelName;
            }
        }
        return null;
    }

    /**
     * 建立与交通部链路
     *
     * @param platId 平台id
     * @param ip     交通部ip
     * @param port   交通部port
     * @return 连接成功返回监听的通道名，否则返回null
     */
    @Override
    public String connectTOM(String platId, String ip, int port) {
        String channelName = platId + transferLinkSuffix;
        //todo 调用链路管理接口开启端口
        int result = chainLinkConsumer.createChannel(ip, port, channelName);
        if (result == 0) {
            //建立链路后开始监听
            if (receiveMsgReceiveTransferClientMsg.listen(channelName)) {
                return channelName;
            }
        }
        return null;
    }

    /**
     * 建立与2.0从链路
     *
     * @param platId 平台id
     * @param ip     2.0ip
     * @param port   2.0port
     * @return 连接成功返回监听的通道名，否则返回null
     */
    @Override
    public String connectVMS(String platId, String ip, int port) {
        String channelName = platId + slaveLinkSuffix;
        //todo 调用链路管理接口开启端口
        int result = chainLinkConsumer.createChannel(ip, port, channelName);
        if (result == 0) {
            //建立链路后开始监听
            if (receiveMsgReceive809ClientMsg.listen(channelName)) {
                return channelName;
            }
        }
        return null;
    }

    /**
     * 关闭链路
     *
     * @param platId 要关闭链路的平台id
     * @param flag   链路标识 1：主链路，2：从链路，3：交通部链路
     * @return 关闭成功返回true，否则返回false
     */
    @Override
    public Boolean close(String platId, int flag) {

        if (platId != null) {
            String channelName = platId;
            switch (flag) {
                case 1:
                    channelName += mainLinkSuffix;
                    //todo 调用链路管理接口关闭链路
                    int integer1 = chainLinkConsumer.closeChannel(channelName);
                    if (integer1 == 0) {
                        return true;
                    }
                    break;
                case 2:
                    channelName += slaveLinkSuffix;
                    //todo 调用链路管理接口关闭链路
                    int integer2 = chainLinkConsumer.closeChannel(channelName);
                    if (integer2 == 0) {
                        return true;
                    }
                    break;
                case 3:
                    channelName += transferLinkSuffix;
                    //todo 调用链路管理接口关闭链路
                    int integer3 = chainLinkConsumer.closeChannel(channelName);
                    if (integer3 == 0) {
                        return true;
                    }
                    break;
            }
        }
        return false;
    }
}
